#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_pLoadingLabel = ui->label;
    m_pAnimation = new QPropertyAnimation(this, "pixmap");
    m_pAnimation->setDuration(1000);
    // 永远运行，直到stop
    m_pAnimation->setLoopCount(-1);

    // 设置动画各区间的值
//    for (int i = 0; i < 8; ++i)
//    {
//        m_pAnimation->setKeyValueAt(i/7.0, i + 1);
//    }

    // 设置动画各区间的值
    for (int i = 0; i <= 10; ++i)
    {
        m_pAnimation->setKeyValueAt(i / 10.0, i);
    }

    // 开始动画
    startAnimation();
}

void MainWindow::startAnimation()
{
    m_pAnimation->start();
}

void MainWindow::stopAnimation()
{
    m_pAnimation->stop();
}

int MainWindow::pixmap() const
{
    return m_nIndex;
}

void MainWindow::setPixmap(const int index)
{
//    m_nIndex = index;
//    QPixmap pixmap(QString(":/Images/loading%1").arg(index));
//    m_pLoadingLabel->setPixmap(pixmap);
    m_nIndex = index;

    // 图标旋转
    QMatrix leftmatrix;
    leftmatrix.rotate((360.0 / 10) * index);

    QPixmap pixmap(QString(":/Images/loading1"));
    QPixmap pix = pixmap.transformed(leftmatrix, Qt::SmoothTransformation);
    m_pLoadingLabel->setPixmap(pix);
}

MainWindow::~MainWindow()
{
    delete ui;
}
